﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;

public partial class main_uc_comp_fav_main : DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeUI();
        if (!Page.IsPostBack)
        {
            LoadCompany();
        }
    }

    private void LoadCompany()
    {
        long cid = 0;
        if (Request.Cookies["texchange"]["cid"] != null && Request.Cookies["texchange"]["cid"].ToString() != "")
        {
            try
            {
                cid = Convert.ToInt64(DAO.GlobalSettings.Decrypt(Request.Cookies["texchange"]["cid"].ToString()));
            }
            catch { }
        }
        if (cid > 0)
        {
            int page = 1;
            if (Request.QueryString["page"] != null && Request.QueryString["page"] != "")
                Int32.TryParse(Request.QueryString["page"], out page);
            /*int catid = 0;
            if (Request.QueryString["catid"] != null && Request.QueryString["catid"] != "")
                Int32.TryParse(Request.QueryString["catid"], out catid);
            int ttype = 0;
            if (Request.QueryString["ttype"] != null && Request.QueryString["ttype"] != "")
                Int32.TryParse(Request.QueryString["ttype"], out ttype);
            string name = "";
            if (Request.QueryString["name"] != null && Request.QueryString["name"] != "")
                name = Request.QueryString["name"];
            int countryid = 0;
            if (Request.QueryString["countryid"] != null && Request.QueryString["countryid"] != "")
                Int32.TryParse(Request.QueryString["ttype"], out countryid);*/
            int start = 1;
            int limit = 15;
            if (page != 1)
            {
                start = limit * (page - 1);
                limit = (limit * (page - 1)) + 15;
            }
            using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SP_GetAllCompanyFavCount", conn))
                {
                    cmd.Parameters.Add("lang", this.LangId);
                    cmd.Parameters.Add("start", 1);
                    cmd.Parameters.Add("limit", 100000);
                    cmd.Parameters.Add("order", "id");
                    cmd.Parameters.Add("orderdir", "asc");
                    cmd.Parameters.Add("cid", cid);
                    /*if (name == "")
                        cmd.Parameters.Add("name", null);
                    else cmd.Parameters.AddWithValue("name", "%" + name + "%");
                    if (catid == 0)
                        cmd.Parameters.Add("catid", null);
                    else cmd.Parameters.Add("catid", catid);
                    if (ttype == 0)
                        cmd.Parameters.Add("ttype", null);
                    else cmd.Parameters.Add("ttype", ttype);
                    if (countryid == 0)
                        cmd.Parameters.Add("countryid", null);
                    else cmd.Parameters.Add("countryid", countryid);*/
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    object val = cmd.ExecuteScalar();//total records
                    if (limit > (int)val)
                    {
                        limit = (int)val;
                    }
                    double pages = Math.Ceiling(Convert.ToDouble(val) / 10);
                    HyperLink hl;
                    int _pages = (int)pages;
                    for (int i = 1; i <= _pages; i++)
                    {
                        hl = new HyperLink();
                        hl.Text = i.ToString();
                        hl.CssClass = "lblInfo";
                        hl.NavigateUrl = "~/Favorites.aspx?page=" + i.ToString();
                        if (i != 1 && i != (_pages + 1))
                            phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                        phNav.Controls.Add(hl);
                        if ((i == pages) && page != _pages)
                        {
                            phNav.Controls.Add(new LiteralControl("<span>&nbsp;|&nbsp;</span>"));
                            hl = new HyperLink();
                            hl.Text = DAO.TextBaseFactory.loadText("next", this.LangId);
                            hl.CssClass = "lblInfo";
                            hl.NavigateUrl = "~/Favorites.aspx?page=" + page + 1;
                            phNav.Controls.Add(hl);
                        }
                    }
                    if ((int)val == 0)
                    {
                        lblInfo.Text = DAO.TextBaseFactory.loadText("no_records", this.LangId);
                    }
                    else lblInfo.Text = DAO.TextBaseFactory.loadText("showing", this.LangId) + " " + start + "-" + limit + " " + DAO.TextBaseFactory.loadText("of", this.LangId) + " " + val + " " + DAO.TextBaseFactory.loadText("total_res", this.LangId);
                    cmd.Parameters.Clear();
                    cmd.CommandText = "SP_GetAllFavoriteCompany";
                    cmd.Parameters.Add("lang", this.LangId);
                    cmd.Parameters.Add("start", 1);
                    cmd.Parameters.Add("limit", 100000);
                    cmd.Parameters.Add("order", "id");
                    cmd.Parameters.Add("orderdir", "asc");
                    cmd.Parameters.Add("cid", cid);
                    /*if (name == "")
                        cmd.Parameters.Add("name", null);
                    else cmd.Parameters.AddWithValue("name", "%" + name + "%");
                    if (catid == 0)
                        cmd.Parameters.Add("catid", null);
                    else cmd.Parameters.Add("catid", catid);
                    if (ttype == 0)
                        cmd.Parameters.Add("ttype", null);
                    else cmd.Parameters.Add("ttype", ttype);
                    if (countryid == 0)
                        cmd.Parameters.Add("countryid", null);
                    else cmd.Parameters.Add("countryid", countryid);*/
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    SqlDataReader dr = cmd.ExecuteReader();
                    StringBuilder sb;
                    DateTime dt;
                    bool _new = false;
                    string _cssClass;
                    while (dr.Read())
                    {
                        DateTime.TryParse(dr["created"].ToString(), out dt);
                        sb = new StringBuilder();
                        sb.Append("<div class=\"company\"><div class=\"company_header\">");
                        if (DateTime.Compare(dt, Utilities.GetStartOfLastWeek()) >= 0)
                        {
                            sb.Append("<div class=\"comp_new\"></div>");
                            _new = true;
                        }
                        string _cname = (dr["country_name"].ToString().Length > 12) ? dr["country_name"].ToString().Substring(0, 12) + "..." : dr["country_name"].ToString();
                        _cssClass = _new ? "comp_header_type" : "comp_header_type1";
                        sb.Append("<div class=\"comp_header_name\">" + dr["name"] + "</div>");
                        switch (dr["member_type_id"].ToString())
                        {
                            case "1":
                                sb.Append("<div class='" + _cssClass + "'>* * * " + DAO.TextBaseFactory.loadText("gold_member", this.LangId) + "</div></div>");
                                break;
                            case "2":
                                sb.Append("<div class='" + _cssClass + "'>* * * " + DAO.TextBaseFactory.loadText("silver_member", this.LangId) + "</div></div>");
                                break;
                            case "3":
                                sb.Append("<div class='" + _cssClass + "'>* * * " + DAO.TextBaseFactory.loadText("bronze_member", this.LangId) + "</div></div>");
                                break;
                            case "0":
                                sb.Append("<div class='" + _cssClass + "'>* * * " + DAO.TextBaseFactory.loadText("_member", this.LangId) + "</div></div>");
                                break;
                            default:
                                sb.Append("<div class='" + _cssClass + "'>* * * " + DAO.TextBaseFactory.loadText("_member", this.LangId) + "</div></div>");
                                break;
                        }
                        sb.Append("<div class=\"company_main\"><div class=\"company_logo\">");
                        sb.Append("<img width=\"53px\" height=\"39px\" src=\"design_img/CompanyLogoDefault.jpg\" class=\"padding_\" /></div>");
                        sb.Append("<div class=\"comp_details1\"><p><strong>" + DAO.TextBaseFactory.loadText("country", this.LangId) + "</strong></p><p style='text-align: left;'><img height='12' width='20' src='images/countries/" + dr["short"].ToString() + "-t.gif' title='" + dr["country_name"].ToString() + "' />&nbsp;&nbsp;" + dr["short"].ToString() + " - " + _cname + "</p></div>");
                        sb.Append("<div class=\"comp_details2\"><p><strong>" + DAO.TextBaseFactory.loadText("city", this.LangId) + "</strong></p><p>" + dr["City"].ToString() + "</p></div><div class=\"op_activity\"><p><strong>" + DAO.TextBaseFactory.loadText("operating_activ", this.LangId) + "</strong></p>");
                        sb.Append("<p>" + dr["operating_activity"].ToString() + "</p></div><div class=\"compType\"><p><strong>" + DAO.TextBaseFactory.loadText("company_type", this.LangId) + "</strong></p>");
                        sb.Append("<p>" + dr["companyType"].ToString() + "</p></div>");
                        sb.Append("<div class=\"compMore\"><div class=\"viewInfoLink\"><a href=\"CompanyDetails.aspx?cid=" + dr["id"].ToString() + "\" class=\"viewInfoLink\" >" + DAO.TextBaseFactory.loadText("comp_info_det", this.LangId) + "</a></div>");
                        sb.Append("</div></div></div>");
                        phCompany.Controls.Add(new LiteralControl(sb.ToString()));
                    }
                }
            }
        }
    }
    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }
}
